package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.query.MenuQuery;
import cn.wolfcode.crm.service.IMenuService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;
    
    @RequestMapping("/view")
    public String index(Model model){
        return "system/menu";
    }


    //菜单界面
    @RequestMapping("list")
    @ResponseBody
    public List<Menu> list(MenuQuery qo){
        return menuService.query(qo);

    }

    @RequestMapping("returnUp")
    @ResponseBody
    public List<Menu> returnUp(MenuQuery qo){
        //显示当前菜单层级
        if (qo.getParentId() != null) {
            //获取所有的父菜单
            return menuService.getPatentMenus(qo.getParentId());
        }
        return null;
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Menu entity){
        JsonResult jsonResult = new JsonResult();
        try {
            menuService.saveOrUpdate(entity);
            return jsonResult;
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("保存失败");
            return jsonResult;
        }
    }

    @RequestMapping("/selectRootMenu")
    @ResponseBody
    public Object selectRootMenu(){
        return menuService.selectRootMenu();
    }

    @RequestMapping("/selectByRoleId")
    @ResponseBody
    public Object selectByRoleId(Long id){
        
        return menuService.selectByRoleId(id);
    }



    // 根据 用户 获取 菜单
    @RequestMapping("/selectEmployeeMenu")
    @ResponseBody
    public Object selectEmployeeMenu(){
        List<Menu> menus = menuService.selectEmployeeMenu();
        return menus;
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult jsonResult = new JsonResult();
        try {
            menuService.delete(id);
            return jsonResult;
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("保存失败");
            return jsonResult;
        }
    }

}
